<?php
/**
 * Creater: 李青林
 * Date Time : 2018/9/20 16:11
 */

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\User;

class UsercapitalController extends Controller
{
    use ModelForm;
    protected $headerStr = '用户积分';

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->description('编辑' . $this->headerStr);
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->description($this->headerStr . '列表...');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->headerStr . '管理');
            $content->description('编辑' . $this->headerStr);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->headerStr . '管理');
            $content->description('新增' . $this->headerStr);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {
            $grid->model()->select('users.id','users.name', 'users.avatar', 'users_capital.voucher_number', 'users_capital.integral')
                ->rightjoin('users_capital', 'users.id', '=', 'users_capital.user_id')
                ->where('users_capital.integral', '=', 0);
            $grid->id('编号')->sortable();
            $grid->name('昵称');
            $grid->avatar('头像')->image(40, 40);
            $grid->integral('积分余额');
            $grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->disableActions();
            $grid->paginate(env('Admin_Limit'));
            $grid->filter(function ($filter) {
                $filter->like('name', '用户昵称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->text('phone', '手机号')->rules('required');
            $form->text('name', '昵称')->rules('required');
            $form->image('avatar', '头像');
            $form->text('signature', '个性签名');
            $form->password('password', '登录密码')->rules('required|confirmed');
            $form->password('password_confirmation', '确认密码')->rules('required')
                ->default(function ($form) {
                    return $form->model()->password;
                });
            $form->ignore(['password_confirmation']);
            $form->display('created_at', '创建时间');
            $form->display('updated_at', '修改时间');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
            $form->saving(function (Form $form) {
                if ($form->password && $form->model()->password != $form->password) {
                    $form->password = bcrypt(md5(md5("q05RbDInqSNNhMWuLE" . $form->password)));
                }
            });
        });
    }

}